//
//  OperationCollectView.m
//  HealthA段
//
//  Created by MrJXT on 15/6/11.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "OperationCollectView.h"
#import "dataBaseHandle.h"
#import "OperationDatilViewController.h"
#import "CollectCell.h"
#import "UIImageView+WebCache.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface OperationCollectView ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,retain)UITableView *CollectView;
@property(nonatomic,retain)NSMutableArray *CollectArr;
@property(nonatomic,retain)UIAlertView *clearAlear;
@end

@implementation OperationCollectView
- (void)dealloc
{
    [_clearAlear release];
    [_CollectArr release];
    [_CollectView release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.CollectArr = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[dataBaseHandle shareDB]openDB];
    
    self.CollectArr = [[dataBaseHandle shareDB]selectOperationTable];
    // Do any additional setup after loading the view.
    self.view.backgroundColor= [UIColor cyanColor];
    [self creatKnowCollectView];
    self.title = @"手术项目";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"清空" style:UIBarButtonItemStylePlain target:self action:@selector(clear:)];
}
//清空按钮
-(void)clear:(UIBarButtonItem *)bu
{
    self.clearAlear = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定要清空收藏吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [self.clearAlear show];
    [_clearAlear release];
    NSLog(@"清空");
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
    }else{
        [[dataBaseHandle shareDB]dropOperationTable];
        [self.CollectArr removeAllObjects];
        [self.CollectView reloadData];
    }
}

//创建手术醒目收藏视图
-(void)creatKnowCollectView
{
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    UIImage *name = [UIImage imageNamed:@"jianchashoucang.jpg"];
    image.image = name;
    self.CollectView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) style:UITableViewStylePlain];
    [self.view addSubview:self.CollectView];
    [self.CollectView setBackgroundColor:[UIColor clearColor]];
    [self.CollectView setBackgroundView:image];
    self.CollectView.delegate = self;
    self.CollectView.dataSource = self;
    self.CollectView.rowHeight = 80;
    self.CollectView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_CollectView release];
}
#pragma mark 实现协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.CollectArr count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    CollectCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[CollectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
    }
    NSURL *url = [NSURL URLWithString:[self.CollectArr[indexPath.row]img]];
    [cell.picView sd_setImageWithURL:url];
    cell.nameLabel.text = [self.CollectArr[indexPath.row] title];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setBackgroundColor:[UIColor clearColor]];
    return cell;
}
//调回详情页面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OperationDatilViewController *kdvc = [[OperationDatilViewController alloc]init];
    kdvc.id = [self.CollectArr[indexPath.row] id];
    kdvc.title = [self.CollectArr[indexPath.row] name];
    [self.navigationController pushViewController:kdvc animated:YES];
    [kdvc release];
}

#pragma mark 滑动删除


-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"%ld",indexPath.row);
        [[dataBaseHandle shareDB] creatOperationTable];
        [[dataBaseHandle shareDB]deleteOperationTableWithTitle:[self.CollectArr[indexPath.row]title]];
        [self.CollectArr removeObjectAtIndex:[indexPath row]];
        [self.CollectView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
        [self.CollectView reloadData];
    }
    
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
